using System;
using WindowsPhoneMVC.Interfaces;
using WindowsPhoneMVC.Navigation;
using WindowsPhoneMVC.Phone.Interfaces;

namespace WindowsPhoneMVC.Tests.Navigation.Fakes
{
    public class FakeNavigator : Navigator
    {
        private IJournal navigationJournal;

        public FakeNavigator() : base(null, null, null, null, null)
        {
        }

        public override void Initialise(IJournal journal, IMvcFrame frame, string frameName)
        {
            navigationJournal = journal;
            if (InitialiseAction != null)
                InitialiseAction(journal, frame, frameName);
        }

        public Action<IJournal, IMvcFrame, string> InitialiseAction { get; set; }

        public string CurrentController { get; private set; }
        public string CurrentAction { get; private set; }

        public override void Invoke(NavigationRequest request, NavigationDirection direction = NavigationDirection.Forward)
        {
            CurrentController = request.ControllerName;
            CurrentAction = request.ActionName;
            navigationJournal.PushEntry(request);
        }

        public override bool NavigateBack()
        {
            return navigationJournal.PopEntry() != null;
        }

        public override void RemoveBackEntry()
        {
            navigationJournal.PopEntry();
        }

        public override IMvcFrame NavigationFrame
        {
            get { return null; }
        }

        public override string NavigationFrameName
        {
            get { return "TestFrame"; }
        }

        public override bool CancelNavigation()
        {
            if (navigationJournal.IsEmpty())
                return false;

            OnNavigationCancelled(EventArgs.Empty);
            return true;
        }

        public override void SaveState()
        {
            
        }
    }
}